package org.sgx.gwtsjcl.client;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * wrapps the sjcl object
 * 
 * @see http://bitwiseshiftleft.github.com/sjcl/doc/symbols/sjcl.html
 * @author sg
 * 
 */
public class SJCL extends JavaScriptObject {
	protected SJCL() {
	}

	/**
	 * return the sjcl global namespace object
	 * 
	 * @return
	 */
	public static final native SJCL sjcl()/*-{
		return $wnd["sjcl"];
	}-*/;

	/**
	 * Simple decryption function.
	 * 
	 * @param password
	 *            The password or key.
	 * @param ciphertext
	 *            The ciphertext to decrypt.
	 * @return
	 */
	public final native String decrypt(String password, String ciphertext)/*-{
		return this.decrypt(password, ciphertext);
	}-*/;

	/**
	 * Simple encryption function.
	 * 
	 * @param password
	 *            The password or key.
	 * @param plainText
	 *            The data to encrypt.
	 * @return
	 */
	public final native String encrypt(String password, String plainText)/*-{
		return this.encrypt(password, plainText);
	}-*/;

	/**
	 * Symmetric ciphers.
	 * 
	 * @return
	 */
	public native final Cypher cypher() /*-{
		return this["cypher"];
	}-*/;

	/**
	 * Symmetric ciphers.
	 * 
	 * @param val
	 * @return this - for setter chaining
	 */
	public native final SJCL cypher(Cypher val) /*-{
		this["cypher"] = val;
		return this;
	}-*/;

	/**
	 * Arrays of bits, encoded as arrays of Numbers.
	 * 
	 * @return
	 */
	public native final BitArray bitArray() /*-{
		return this["bitArray"];
	}-*/;

	/**
	 * Arrays of bits, encoded as arrays of Numbers.
	 * 
	 * @param val
	 * @return this - for setter chaining
	 */
	public native final SJCL bitArray(BitArray val) /*-{
		this["bitArray"] = val;
		return this;
	}-*/;

	/**
	 * Bit array encoders and decoders.
	 * 
	 * @return
	 */
	public native final Codec codec() /*-{
		return this["codec"];
	}-*/;

	/**
	 * Bit array encoders and decoders.
	 * 
	 * @param val
	 * @return this - for setter chaining
	 */
	public native final SJCL codec(Codec val) /*-{
		this["codec"] = val;
		return this;
	}-*/;

	/**
	 * sjcl.random - Random number generator
	 * 
	 * <p>
	 * This random number generator is a derivative of Ferguson and Schneier's generator Fortuna. It collects entropy from various events into several pools, implemented by
	 * streaming SHA-256 instances. It differs from ordinary Fortuna in a few ways, though.
	 * </p>
	 * 
	 * <p>
	 * Most importantly, it has an entropy estimator. This is present because there is a strong conflict here between making the generator available as soon as possible, and making
	 * sure that it doesn't "run on empty". In Fortuna, there is a saved state file, and the system is likely to have time to warm up.
	 * </p>
	 * 
	 * <p>
	 * Second, because users are unlikely to stay on the page for very long, and to speed startup time, the number of pools increases logarithmically: a new pool is created when
	 * the previous one is actually used for a reseed. This gives the same asymptotic guarantees as Fortuna, but gives more entropy to early reseeds.
	 * </p>
	 * 
	 * <p>
	 * The entire mechanism here feels pretty klunky. Furthermore, there are several improvements that should be made, including support for dedicated cryptographic functions that
	 * may be present in some browsers; state files in local storage; cookies containing randomness; etc. So look for improvements in future versions.
	 * </p>
	 * 
	 * @return
	 */
	public native final Random random() /*-{
		return this["random"];
	}-*/;

	/**
	 * sjcl.random - Random number generator
	 * 
	 * <p>
	 * This random number generator is a derivative of Ferguson and Schneier's generator Fortuna. It collects entropy from various events into several pools, implemented by
	 * streaming SHA-256 instances. It differs from ordinary Fortuna in a few ways, though.
	 * </p>
	 * 
	 * <p>
	 * Most importantly, it has an entropy estimator. This is present because there is a strong conflict here between making the generator available as soon as possible, and making
	 * sure that it doesn't "run on empty". In Fortuna, there is a saved state file, and the system is likely to have time to warm up.
	 * </p>
	 * 
	 * <p>
	 * Second, because users are unlikely to stay on the page for very long, and to speed startup time, the number of pools increases logarithmically: a new pool is created when
	 * the previous one is actually used for a reseed. This gives the same asymptotic guarantees as Fortuna, but gives more entropy to early reseeds.
	 * </p>
	 * 
	 * <p>
	 * The entire mechanism here feels pretty klunky. Furthermore, there are several improvements that should be made, including support for dedicated cryptographic functions that
	 * may be present in some browsers; state files in local storage; cookies containing randomness; etc. So look for improvements in future versions.
	 * </p>
	 * 
	 * @param val
	 * @return this - for setter chaining
	 */
	public native final SJCL random(Random val) /*-{
		this["random"] = val;
		return this;
	}-*/;

	/**
	 * Hash functions. Right now only SHA256 is implemented.
	 * 
	 * @return
	 */
	public native final Hash hash() /*-{
		return this["hash"];
	}-*/;

	/**
	 * Hash functions. Right now only SHA256 is implemented.
	 * 
	 * @param val
	 * @return this - for setter chaining
	 */
	public native final SJCL hash(Hash val) /*-{
		this["hash"] = val;
		return this;
	}-*/;
	
	/**
	 * JSON encapsulation 
	 * @return
	 */
	public native final JSON json() /*-{
		return this["json"]; 
	}-*/;
	
	/**
	 * JSON encapsulation 
	 * @param val
	 * @return this - for setter chaining
	 */
	public native final SJCL json(JSON val) /*-{
		this["json"] = val; 
		return this; 
	}-*/;
	
	/**
	 * Miscellaneous. HMAC and PBKDF2. 
	 * @return
	 */
	public native final Misc misc() /*-{
	return this["misc"]; 
	}-*/;
	
	/**
	 * Miscellaneous. HMAC and PBKDF2. 
	 * @param val
	 * @return this - for setter chaining
	 */
	public native final SJCL misc(Misc val) /*-{
	this["misc"] = val; 
	return this; 
	}-*/;
}
